package com.project1.cmpe473;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CompanyForm extends HttpServlet {
	public static ArrayList<Company> companyList = new ArrayList<Company>();
	
	private static final long serialVersionUID = 1L;
    public CompanyForm() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		RequestDispatcher dispatcher =   getServletContext().getRequestDispatcher("/companyForm.jsp");
		dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String error="";
		try{
			
			//gets the name field and checks required name field filled
			String name=request.getParameter("name");
			if(name.equals(""))
				error="Name cannot be empty";
			
			//gets the university field
			String university=request.getParameter("university");
			
			//gets the gpa field and checks valid format
			String gpaString=request.getParameter("gpa");
			double gpa=0;
			if(!gpaString.equals(""))
			{
				if (!gpaString.matches("^[1-9]\\d*(.\\d+)?$") && !gpaString.matches("^[1-9]\\d*(,\\d+)?$")) {
				    error="Invalid gpa";
				}
				else
				{
					gpaString=gpaString.replace(',', '.');
					gpa=Double.parseDouble(gpaString);
				}
			}
			
			//gets the degree field and checks valid format
			// Bs=1, Ms=2, phd=3
			String[] degreeArray = request.getParameterValues("degree");
			ArrayList<Integer> degreeList= new ArrayList<Integer>();
			if (degreeArray != null && degreeArray.length != 0)
			{
				for(String degree : degreeArray)
				{
					degreeList.add(Integer.parseInt(degree));
				}
			}
			else
				error="Invalid degree";
			
			//gets the experience field and checks valid format
			String expString=request.getParameter("experience");
			int experience=0;
			if(!expString.equals(""))
			{
				if (!expString.matches("^\\d+$")) {
				    error="Invalid experience";
				}
				else
					experience= Integer.parseInt(expString);
			}
			
			//gets the gender field and checks valid format
			String[] genderArray = request.getParameterValues("gender");
			ArrayList<String> genderList= new ArrayList<String>();
			if (genderArray != null && genderArray.length != 0)
			{
				for(String gender : genderArray)
				{
					genderList.add(gender);
				}
			}
			else
				error="Invalid gender";
			
			if(error!="")//there is an error
			{
				request.setAttribute("error",error);
				
				RequestDispatcher dispatcher =   getServletContext().getRequestDispatcher("/companyForm.jsp");
				dispatcher.forward(request, response);
			}
			else
			{		
				
				Company company=new Company(companyList.size() ,name,university,gpa,degreeList.get(0),experience,genderList);
				
				//RMI Part
				Registry registry = LocateRegistry.getRegistry();
				registry.rebind("Company"+company.id, (Approval)company );
				
				//adds company object to companyList global variable
				companyList.add(company);
				
				RequestDispatcher dispatcher =   getServletContext().getRequestDispatcher("/companyForm.jsp");
				dispatcher.forward(request, response);
			}
		}
		catch (Exception e) {
			error="An error occured: "+e.getMessage();
			request.setAttribute("error",error);
			
			RequestDispatcher dispatcher =   getServletContext().getRequestDispatcher("/companyForm.jsp");
			dispatcher.forward(request, response);
		}
	}
}